<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="计量单位代码" prop="wmsMeasuringUnit">
        <el-input
          v-model="queryParams.wmsMeasuringUnit"
          placeholder="计量单位代码模糊查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="换算率" prop="conversionRate">
        <el-input
          v-model="queryParams.conversionRate"
          placeholder="请输入换算率"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="备注" prop="docNote">
        <el-input
          v-model="queryParams.docNote"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:unitConversion:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:unitConversion:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:unitConversion:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:unitConversion:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="unitConversionList" @selection-change="handleSelectionChange" stripe :highlight-current-row="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55"   align="center">
        <template scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <el-table-column label="物料名称" align="center" prop="itemName" />
      <el-table-column label="物料编码" align="center" prop="itemNum" width="120"/>
      <el-table-column label="基本计量单位" align="center" prop="wmsMeasuringUnitBasic" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_dict_measuring_unit" :value="scope.row.wmsMeasuringUnitBasic"/>
        </template>
      </el-table-column>
      <el-table-column label="基本计量单位代码" align="center" prop="wmsMeasuringUnitBasic" width="150"/>
      <el-table-column label="换算计量单位" align="center" prop="wmsMeasuringUnitConversion" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_dict_measuring_unit_conversion" :value="scope.row.wmsMeasuringUnitConversion"/>
        </template>
      </el-table-column>
      <el-table-column label="换算计量单位代码" align="center" prop="wmsMeasuringUnitConversion" width="150"/>
      <el-table-column label="换算率" align="center" prop="conversionRate" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="attentionUserName" />
      <el-table-column label="备注" align="center" prop="docNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:unitConversion:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:unitConversion:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改换算对话框 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="450px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料" prop="fkItemUuid">
          <el-input v-model="form.fkItemUuid" v-show="false"/>
          <el-input placeholder="请点击右侧查找按钮选择物料" v-model="form.selectedItem" class="input-with-select"
                    :style="{width: '100%'}">
            <el-button @click="dialog_show_selectItem"
                       slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="基本单位" prop="wmsMeasuringUnitBasic">
          <el-select v-model="form.wmsMeasuringUnitBasic" placeholder="请选择基本计量单位" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.wms_dict_measuring_unit"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="换算单位" prop="wmsMeasuringUnitConversion">
          <el-select v-model="form.wmsMeasuringUnitConversion" placeholder="请选择换算计量单位" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.wms_dict_measuring_unit_conversion"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="换算率" prop="conversionRate">
          <el-input v-model="form.conversionRate" placeholder="请输入换算率" />
        </el-form-item>
        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="form.sysNormalDisable" placeholder="请选择状态" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="经办人" prop="fkAttentionUserId">
          <el-input v-model="form.fkAttentionUserId" v-show="false"/>
          <el-input placeholder="请选择经办人" v-model="form.selectedAttentionUser" class="input-with-select">
            <el-button @click="dialog_show_selectUser" :style="{width: '100%'}" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="备注" prop="docNote">
          <el-input v-model="form.docNote" type="textarea" autosize placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <select-user ref="component_selectUser" @ok="selectUser_done"/>
    <select-item ref="component_selectItem" @ok="selectItem_done"/>
  </div>
</template>

<script>
import { listUnitConversion, getUnitConversion, delUnitConversion, addUnitConversion, updateUnitConversion } from "@/api/wms/unitConversion";
import selectUser from "../../comn/userGroup/selectUser";
import selectItem from "../../wms/wmsItems/selectItem";
export default {
  name: "UnitConversion",
  dicts: ['wms_dict_measuring_unit', 'wms_dict_measuring_unit_conversion', 'sys_normal_disable'],
  components: { selectUser,selectItem },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 换算表格数据
      unitConversionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mucUuid: null,
        fkItemUuid: null,
        wmsMeasuringUnit: null,
        wmsMeasuringUnitBasic: null,
        wmsMeasuringUnitBasicCode: null,
        wmsMeasuringUnitConversion: null,
        wmsMeasuringUnitConversionCode: null,
        conversionRate: null,
        sysNormalDisable: null,
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: null,
        confirmNote: null,
        sysYesNoDone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mucUuid: [
          { required: true, message: "主键不能为空", trigger: "change" }
        ],
        fkItemUuid: [
          { required: true, message: "物料id不能为空", trigger: "change" }
        ],
        wmsMeasuringUnitBasic: [
          { required: true, message: "基本计量单位不能为空", trigger: "change" }
        ],
        wmsMeasuringUnitConversion: [
          { required: true, message: "换算计量单位不能为空", trigger: "change" }
        ],
        conversionRate: [
          { required: true, message: "换算率不能为空", trigger: "change" }
        ],
        sysNormalDisable: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        fkAttentionUserId: [
          { required: true, message: "经办人ID不能为空", trigger: "change" }
        ],
        oaDictConfirmStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    indexMethod(index){
      return (this.currentPage-1)*this.pageSize+index+1
    },

    /** 查询换算列表 */
    getList() {
      this.loading = true;
      listUnitConversion(this.queryParams).then(response => {
        this.unitConversionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /**取消按钮*/
    cancel() {
      this.open = false;
      this.reset();
    },

    /**表单重置*/
    reset() {
      this.form = {
        mucUuid: null,
        fkItemUuid: null,
        wmsMeasuringUnitBasic: null,
        wmsMeasuringUnitBasicCode: null,
        wmsMeasuringUnitConversion: null,
        wmsMeasuringUnitConversionCode: null,
        conversionRate: null,
        sysNormalDisable: "0",
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: "0",
        confirmNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        sysYesNoDone: null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /**多选框选中数据*/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mucUuid)
      this.itemNames = selection.map(item => item.itemName)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增单位换算";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mucUuid = row.mucUuid || this.ids
      getUnitConversion(mucUuid).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.form.selectedAttentionUser = `${this.form.attentionUserName}（${this.form.fkAttentionUserId}）`
        this.form.selectedItem = `${this.form.itemName}（${this.form.itemNum}）`
        this.open = true;
        this.title = "修改单位换算";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.mucUuid != null) {
            updateUnitConversion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnitConversion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const mucUuids = row.mucUuid || this.ids;
      const itemNames = row.itemName || this.itemNames;
      this.$modal.confirm('是否确认删除换算物料名称为"' + itemNames+ '"的数据？','删除确认').then(function() {
        return delUnitConversion(mucUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/wms/unitConversion/export', {
        ...this.queryParams
      }, `wms_unitConversion.xlsx`)
    },

    /** 打开经办人选择弹窗 */
    dialog_show_selectUser() {
      this.$refs.component_selectUser.show();
    },

    /** 完成：选择经办人 */
    selectUser_done(user) {
      this.form.fkAttentionUserId = user.userId
      this.form.selectedAttentionUser = `${user.nickName}（${user.userId}）`
    },

    /** 打开物料选择弹窗 */
    dialog_show_selectItem() {
      this.$refs.component_selectItem.dialog_show_selectItem();
    },

    /**完成：选择物料*/
    selectItem_done(item) {
      this.form.fkItemUuid = item.itemUuid
      this.form.selectedItem = `${item.itemName}（${item.itemNum}）`
    },

  }
};
</script>
